   
#include"shell2.h"

void sendM2All(struct user * shared,char * m, int ix){
        int i=0;
                                             
        for(;i<31;i++){
                if(shared[i].using==1){
                        //printf("ffffffffffffffffffffffffffffffffffffffff\n");
                        write(shared[i].socket_fd,m,strlen(m));
                        if(ix!=0&&shared[i].toAll==0){
                        		write(shared[i].socket_fd,"% ",2);
                        }else if(shared[i].toAll==0){
                        	write(shared[i].socket_fd,"\n% ",3);
                        }
                }
        }
}
void who2(int *socket_fd,struct user *shared_stuff){
        //printf("aaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
        int i;
        char buffer[40960]="<ID>\t<nickname>\t<IP/port>\t<indicate me>\n";
        char buffer2[20000];
      //  char s[20];
        for(i=0;i<31;i++){
                if(shared_stuff[i].using==1){
                          sprintf(buffer2,"%d\t%s\t%s/%d\t",i+1,shared_stuff[i].name,shared_stuff[i].ip,shared_stuff[i].port);

                                if(*socket_fd==shared_stuff[i].socket_fd)
                                        strcat(buffer2,"<- me");
                                strcat(buffer2,"\n");
                                strcat(buffer,buffer2);
                        }
                        
        }
                //printf("aaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
                //strcat(buffer,buffer2);
       // strcat(buffer,"% ");
        write(*socket_fd,buffer,strlen(buffer));
}
void setenv2(struct commandInfo *cmd,int *socket_fd,struct user *shared_stuff){
        if(cmd->argv3[0]=='\0')return;
        	int i=0;  
        	int ix;
        	for(;i<32;i++){
        		if(shared_stuff[i].socket_fd==*socket_fd)
        			break;
        	}
        char strsept[]=" \t";
        char *str_ptr=strtok(cmd->argv3,strsept);
              
        char name[2000];
        char value[200];
                strncpy(name,str_ptr,200);
                str_ptr=strtok(NULL,strsept);
                strncpy(value,str_ptr,200);     
                                                
        for(ix=0;ix<ENVNUM;ix++){
                if(!strncmp(name,shared_stuff[i].env[ix].name,200)){
                        strncpy(shared_stuff[i].env[ix].content,value,200);
                        break;
                }
        }
       // write(*socket_fd,"% ",2);
}
int socket_read_line_alloc(int sock, char* out) {
        int i = 0;
        char ch;
        for ( ; ; ) {
                if (recv(sock, &ch, 1, 0) == -1) {
                	
                        return -1;
                }
                if (ch == '\n') {
                        out[i] = '\0';
                        break;
                } else {
                        if(ch=='\r')
                                continue;
                             //  printf("%c\n",ch);
                        out[i] = ch;
                }
                i++;
        }
        return i + 1;
}
void printenv( struct commandInfo *cmd,int *socket_fd,struct user *shared_stuff){
        int i;
        int ix;
        	for(i=0;i<32;i++){
        		if(shared_stuff[i].socket_fd==*socket_fd)
        			break;
        	}
        char buffer[40960];
        if(cmd->argv3!=NULL&&cmd->argv3[0]!='\0'){
                for(ix=0;ix<32;ix++){
                        if(strlen(shared_stuff[i].env[ix].name)!=0&&!strcmp(shared_stuff[i].env[ix].name,cmd->argv3)){
                        	     sprintf(buffer,"%s=%s\n",cmd->argv3,shared_stuff[i].env[ix].content);
                                //strncpy(buffer,cmd->argv3,4095);
                                //strcat(buffer,"=")/*,4095-strlen(cmd->argv3))*/;
                                //strcat(buffer,env[ix].content)/*,4095-strlen(cmd->argv3)-1)*/;
                                //strcat(buffer,"\n")/*,4095-strlen(cmd->argv3)-1-strlen(env[i].content))*/;
                                write(*socket_fd,buffer,strlen(buffer));
                                break;
                        }
                }                                       
        }
}

void sendM(){
	int i;
	                                for(i=0;i<31;i++){
                                        if(users[i].toAll!=0){
                                                if(users[i].toAll==32){
                                                        sendM2All(users,users[i].m,0);
                                                        memset(users[i].m,0,strlen(users[i].m));
                                                      }
                                                else {
                                                        write(users[users[i].toAll-1].socket_fd,users[i].m,strlen(users[i].m));
                                                }
                                                users[i].toAll=0;
                                        }
                        }
                      }

void name(struct commandInfo *cmd,int * socket_fd,struct user *shared_stuff){
        int i;
        for(i=0;i<31;i++){
                if(*socket_fd==shared_stuff[i].socket_fd){
                        strcpy(shared_stuff[i].name,cmd->argv3);
                        break;
                }
        }
        char m[1024];
        sprintf(m,"*** User  from %s/%d is named '%s'. ***\n",shared_stuff[i].ip,shared_stuff[i].port,cmd->argv3);
        //strcpy(shared_stuff[i].m,m);
        shared_stuff[i].toAll=32;
        
        //kill(ppid,SIGUSR1);
        
        sendM2All(shared_stuff,m,1);
        shared_stuff[i].toAll=0;
}
void yell(struct commandInfo *cmd,int * socket_fd,struct user *shared_stuff){
        //char m[10000]="*** ";
        int i;
        for(i=0;i<31;i++){
                if(*socket_fd==shared_stuff[i].socket_fd){
                        break;
                }
        }
        //strcat(m,cmd->argv3);
        sprintf(shared_stuff[i].m,"*** %s yelled ***: %s\n",shared_stuff[i].name,cmd->argv3);
        shared_stuff[i].toAll=32;
        //kill(ppid,SIGUSR1);
        sendM2All(shared_stuff,shared_stuff[i].m,1);
        shared_stuff[i].toAll=0;
}
void tell(struct commandInfo *cmd,int * socket_fd,struct user *shared_stuff){
        int i;
        for(i=0;i<31;i++){
                if(*socket_fd==shared_stuff[i].socket_fd){
                        break;
                }
        }
       // char *  token=strtok(cmd->argv3," ");
        if(atoi(cmd->value)<32&&shared_stuff[atoi(cmd->value)-1].using==1){
                shared_stuff[i].toAll=atoi(cmd->value);
        
                        //char buffer[20480]="*** ";
                        sprintf(shared_stuff[i].m,"*** %s told you ***: %s\n",shared_stuff[i].name,cmd->argv3);
        							//strcat(shared_stuff[i].m,"% ");
                       sendM();
                        //kill(ppid,SIGUSR1);
        }else{
                        char buffer[256];
                         sprintf(buffer,"*** Error: user #%s does not exist yet. ***\n% ",cmd->value);
                       // strcat(buffer,cmd->value);
                        //strcat(buffer," does not exist yet. ***\n% ");
                        write(*socket_fd,buffer,strlen(buffer));
                        
                }

}

void executeBuiltInCommand(struct commandInfo *cmd,int * socket_fd,struct user *shared_stuff){
        if(!strcmp(cmd->commandName,"printenv"))
                printenv(cmd,socket_fd,shared_stuff);
        else if(!strcmp(cmd->commandName,"setenv")){
                setenv2(cmd,socket_fd,shared_stuff);
                
                }
        else if(!strcmp(cmd->commandName,"who")){
                who2(socket_fd,shared_stuff);
                }
        else if(!strcmp(cmd->commandName,"name")){
                name(cmd,socket_fd,shared_stuff);
                }
        else if(!strcmp(cmd->commandName,"yell")){
                yell(cmd,socket_fd,shared_stuff);
                }
        else if(!strcmp(cmd->commandName,"tell")){
                tell(cmd,socket_fd,shared_stuff);
                }
}
int isBuiltInCommand(struct commandInfo * cmd){
        int len=sizeof(builtInCommand)/POINT_SIZE;
        int i;
        for(i=1;i<len;i++)
                if(!strcmp(builtInCommand[i],cmd->commandName))
                        return i;
        return 0;
}

void init_structCommand(int i){
        int ix; 
        for(ix=0;ix<COMMANDNUM;ix++){
                        memset(&(users[i].commandList[ix]),0,sizeof(struct commandInfo));
                        memset(&(users[i].linepipe[ix]),0,sizeof(struct lineInfo));
        }
}
//int comIndex=0;
//int first=0;
void parse(char *cmd,int *client_sockfd,struct user *user,int id){
        int i=0;
        char *str_ptr;
        char strsept[]=" \t";
        char tmp_cmd[20000];
        strcpy(tmp_cmd,cmd);
        str_ptr=strtok(cmd,strsept);
        if(str_ptr==NULL) return;
        if(!strcmp(str_ptr,"yell")){
        	  strncpy(user[id].commandList[user[id].comIndex].commandName,str_ptr,200);
            user[id].commandList[user[id].comIndex++].booluse=1;
              user[id].comIndex%=10000;
        			int i;
        			for(i=4;i<strlen(tmp_cmd);i++)
        				if(tmp_cmd[i]!=' ')
        					break;
        					//printf(cmd);
        					//printf("\n");
        	strcpy(user[id].commandList[user[id].comIndex-1].argv3,&tmp_cmd[i]);
        	 return ;
        }else if(!strcmp(str_ptr,"tell")){
        	strncpy(user[id].commandList[user[id].comIndex].commandName,str_ptr,200);
             user[id].commandList[user[id].comIndex++].booluse=1;
              user[id].comIndex%=10000;
               str_ptr=strtok(NULL,strsept);
              strcpy( user[id].commandList[user[id].comIndex-1].value,str_ptr);
               for(i=4;i<strlen(tmp_cmd);i++)
        				if(tmp_cmd[i]!=' ')
        					break;
        				i+=2;
               for(;i<strlen(tmp_cmd);i++)
        				if(tmp_cmd[i]!=' ')
        					break;
        				strcpy(user[id].commandList[user[id].comIndex-1].argv3,&tmp_cmd[i]);
        	return ;
        }else if(!strcmp(str_ptr,"name")){
        	  strncpy(user[id].commandList[user[id].comIndex].commandName,str_ptr,200);
             user[id].commandList[user[id].comIndex++].booluse=1;
              user[id].comIndex%=10000;
        			int i;
        			for(i=4;i<strlen(tmp_cmd);i++)
        				if(tmp_cmd[i]!=' ')
        					break;
        	strcpy(user[id].commandList[user[id].comIndex-1].argv3,&tmp_cmd[i]);
        	 return ;
        }
        
        
        
        while(str_ptr!=NULL){
                if(!strcmp(str_ptr,"exit")){
                	//printf("ddfdfdfdfdfdf\n");
                        //close(*client_sockfd);
                        //user[id].using=0;
                        //strncpy(user[id].commandList[user[id].comIndex].commandName,str_ptr,200);
                        char m[128];
                        //sprintf(m,"*** User '%s' left. ***\n",user[id].name);
                        //sendM2All(users,m);
                        return;
                        //exit(0);
                        }
                if(str_ptr[0]=='-'){
                        strncpy(user[id].commandList[user[id].comIndex-1].argList,str_ptr,200);
                } 
                else if (str_ptr[0]=='<'){
                				if(str_ptr[1]!='\0'){
											//user[id].commandList[user[id].comIndex-1].is_in=1;
													user[id].commandList[user[id].comIndex-1].ipc_in=(atoi(&str_ptr[1])==0)?-1:atoi(&str_ptr[1]);
												printf("parse %d\n",user[id].commandList[user[id].comIndex-1].ipc_in);
											}else {
												str_ptr=strtok(NULL,strsept);
												if(str_ptr==NULL)
															break;
												strncpy(user[id].commandList[user[id].comIndex-1].inFile,str_ptr,80);
												user[id].commandList[user[id].comIndex-1].boolInFile=1;
		  									}
                      /*  str_ptr=strtok(NULL,strsept);
                        if(str_ptr==NULL)
                                break;
                        strncpy(commandList[comIndex-1].inFile,str_ptr,80);
                        commandList[comIndex-1].boolInFile=1;*/
                }
                else if (str_ptr[0]=='>'){
                	
                				if(str_ptr[1]=='|'){
                					
															user[id].commandList[user[id].comIndex-1].ipc_pipe=1;
												}else if(str_ptr[1]=='!'){
															user[id].commandList[user[id].comIndex-1].ipc_pipe=1;
															user[id].commandList[user[id].comIndex-1].ipc_pipe2=1;
												}else{
															str_ptr=strtok(NULL,strsept);
															if(str_ptr==NULL)
																	break;
															strncpy(user[id].commandList[user[id].comIndex-1].outFile,str_ptr,80);
															user[id].commandList[user[id].comIndex-1].boolOutFile=1;
		  									}
                	
                	
                       /* str_ptr=strtok(NULL,strsept);
                        if(str_ptr==NULL)
                                break;
                        strncpy(commandList[comIndex-1].outFile,str_ptr,80);
                        commandList[comIndex-1].boolOutFile=1;*/
                }
                else if (str_ptr[0]=='|'){
                        i=0;
                        user[id].commandList[user[id].comIndex-1].pipeTo=str_ptr[1]=='\0'?-1:(atoi(&str_ptr[1]));
                }
                else if (str_ptr[0]=='!'){
                        i=0;
                        user[id].commandList[user[id].comIndex-1].pipe2To=str_ptr[1]=='\0'?-1:(atoi(&str_ptr[1]));
                }               
                else {
             //   		printf(str_ptr);
       // printf("\n");
                        if(i==0){
                                strncpy(user[id].commandList[user[id].comIndex].commandName,str_ptr,200);
                                user[id].commandList[user[id].comIndex++].booluse=1;
                               user[id].comIndex%=10000;
                                i=1;
                        }else {
                        //      else{
                                if(strlen(user[id].commandList[user[id].comIndex-1].argv3)!=0){   
                                //	printf("%s   eeeeeeeeeeeeeeeeeeee\n",str_ptr);
                                        strcat(user[id].commandList[user[id].comIndex-1].argv3," ");
                                        strcat(user[id].commandList[user[id].comIndex-1].argv3,str_ptr);
                                }
                                else 
                                        strncpy(user[id].commandList[user[id].comIndex-1].argv3,str_ptr,200);
                       
                        }
                }
                str_ptr=strtok(NULL,strsept);
        }


}
int checkIsExist(struct commandInfo * cmd,char *root,int i,struct user *shared_stuff){
        char tmp_root[1000]; 

        char *  token=strtok(shared_stuff[i].env[0].content,":");
        while(token!=NULL){
        			sprintf(tmp_root,"%s%s/%s",root,token,cmd->commandName);
               // strncpy(tmp_root,root,1000);
                //strcat(tmp_root,token);
                //strcat(tmp_root,"/");
                //strcat(tmp_root,cmd->commandName);
                FILE *fp=fopen(tmp_root,"r");
                if(fp){
                        strncpy(root,tmp_root,200);
                        fclose(fp);
                        return 1;
                }
                token=strtok(NULL,":"); 
        }
        return 0;

}

int main(int argc,char **argv){
//	signal( 11, handler );
        chdir("/home/wei/ras");
        //unsigned int line=0;
        int server_sockfd, client_sockfd;
        int server_len,client_len;
        //char s[100];
        fd_set readfds,testfds;
        //-----------project 2
                //pid_t parentid=getpid();
        struct sockaddr_in server_address;
        struct sockaddr_in client_address;
        server_sockfd=socket(AF_INET,SOCK_STREAM,0);
        server_address.sin_family=AF_INET;
        server_address.sin_addr.s_addr=htonl(INADDR_ANY);
        server_address.sin_port=htons(atoi(argv[1]));
        server_len=sizeof(server_address);
        bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
        listen(server_sockfd,5);
        FD_ZERO(&readfds);
       
        FD_SET(server_sockfd,&readfds);
        memset(users,0,sizeof(struct user)*31);
        while(1){
        	testfds=readfds;
        	int fd;
        	 int nread;
        	int result  = select(31 , &testfds , NULL , NULL , NULL);
        	for(fd=0;fd<31;fd++){
        		if(FD_ISSET(fd,&testfds)){
        			if(fd==server_sockfd){
        				 client_len=sizeof(client_address);
                client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,&client_len);
                FD_SET(client_sockfd,&readfds);
                 int i;
                 for(i=0;i<31;i++){
                        if(users[i].using==0){
                                        users[i].using=1;
                                       break;
                        }
                }
                int y;
        				char welcome[]="****************************************\r\n** Welcome to the information server. **\r\n****************************************\n";
                write(client_sockfd,welcome,strlen(welcome));
                char messages[128];
                sprintf(messages,"*** User '(no name)' entered from nctu/5566",inet_ntoa(client_address.sin_addr),client_address.sin_port);
                users[i].socket_fd=client_sockfd;
                sendM2All(users,messages,0);
                strcpy(users[i].name,"(no name)");
                strcpy(users[i].ip,inet_ntoa(client_address.sin_addr));
        				users[i].port=client_address.sin_port;
                  for(y=0;y<31;y++){
                        memset(users[i].env[y].name,0,sizeof(users[i].env[y].name));
                        memset(users[i].env[y].content,0,sizeof(users[i].env[y].content));
                }
                strncpy(users[i].env[0].name,"PATH",200);
                strncpy(users[i].env[0].content,"bin:.",200);
                
                init_structCommand(i);   
        				}else {
        					
        				//	printf("2222ddfdfdfdfdfdf\n");
                        char cmd[20000];
                        char cmd2[20000];
                        int statval;
                        int i;
                        for(i=0;i<31;i++)
                        	if(fd==users[i].socket_fd)
                        		break;
                        socket_read_line_alloc(fd,cmd);
                        strcpy(cmd2,cmd);
        								users[i].line=users[i].line+1;
        								users[i].first_command=1;
        								parse(cmd,&fd,users,i);
        								if(!strcmp(cmd,"exit")){
        									close(fd);
                        	FD_CLR(fd,&readfds);
                        	char m[128];
        									sprintf(m,"*** User '%s' left. ***",users[i].name);
        									memset(&users[i],0,sizeof(struct user));
                        	sendM2All(users,m,0);
                        }else {
        								if(strlen(cmd)==0)      
                                users[i].comIndex++;
                                int ix;
                                                       for(ix=users[i].first;ix<users[i].comIndex;ix++)
                                                        {
                                                        fprintf(stderr,"[%d]\n",users[i].line);
                                                        fprintf(stderr,"users[%d].comIndex=%d\n",i,users[i].comIndex);
                                                        fprintf(stderr,"users[%d].first=%d\n",i,users[i].first);
                                                        fprintf(stderr,"users[%d].first_command=%d\n",i,users[i].first_command);
                                                        fprintf(stderr,"commandList[%d].boolInfile=%d\n",i,users[i].commandList[ix].boolInFile);
                                                        fprintf(stderr,"commandList[%d].boolOutfile=%d\n",i,users[i].commandList[ix].boolOutFile);
                                                        //fprintf(stderr,"commandList[%d].boolBackground=%d\n",i,commandList[i].boolBackground);
                                                        fprintf(stderr,"commandList[%d].argList=%s\n",i,users[i].commandList[ix].argList);
                                                        fprintf(stderr,"commandList[%d].commandName=%s\n",i,users[i].commandList[ix].commandName);
                                                        fprintf(stderr,"commandList[%d].pipeTo=%d\n",i,users[i].commandList[ix].pipeTo);
                                                        fprintf(stderr,"commandList[%d].pipeIn=%d\n",i,users[i].commandList[ix].pipeIn);
                                                        fprintf(stderr,"commandList[%d].pipe2To=%d\n",i,users[i].commandList[ix].pipe2To);
                                                        fprintf(stderr,"commandList[%d].inFile=%s\n",i,users[i].commandList[ix].inFile);
                                                        fprintf(stderr,"commandList[%d].outFile=%s\n",i,users[i].commandList[ix].outFile);
                                                        fprintf(stderr,"commandList[%d].booluse=%d\n",i,users[i].commandList[ix].booluse);
                                                        fprintf(stderr,"commandList[%d].ipc_pipe=%d\n",i,users[i].commandList[ix].ipc_pipe);
                                                        fprintf(stderr,"commandList[%d].ipc_pipe2=%d\n",i,users[i].commandList[ix].ipc_pipe2);
                                                        fprintf(stderr,"commandList[%d].ipc_in=%d\n",i,users[i].commandList[ix].ipc_in);
                                                        fprintf(stderr,"\n");
                                                        }
                               while(users[i].first!=users[i].comIndex){
                                        if(isBuiltInCommand(&(users[i].commandList[users[i].first]))){
                                        	    //if(!strcmp(users[i].commandList[users[i].first].commandName,"exit")){users[i].first++;break;}
                                                //printf("dddddddddddddddddddddddddddddddddddddddddddddddddddd\n");
                                                executeBuiltInCommand(&(users[i].commandList[users[i].first]),&(users[i].socket_fd),users);
                                                usleep(5);
                                                write(fd,"% ",2);
                                                memset(&(users[i].commandList[users[i].first]),0,sizeof(struct commandInfo));
                                        }
                                        else  {
                                                char buffer[4096]={0};
                                                int pfd[2]={0,0};
                                                if(pipe(pfd)<0)return -1;
                                                switch(users[i].commandList[users[i].first].pipeTo){
                                                        case -1:
                                                                if(users[i].commandList[users[i].first+1].pipeIn==0){
                                                                        if(pipe(users[i].commandList[users[i].first+1].file_pipes)<0){
                                                                                return -1;
                                                                        }
                                                                        else
                                                                                users[i].commandList[users[i].first+1].pipeIn=1;  
                                                                }
                                                        break;
                                                        case 0:
                                                        break;
                                                        default:
                                                           if(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipeTo].using==0){
                                                                if(pipe(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipeTo].file_pipes)<0){
                                                                        return -1;
                                                                }
                                                                else
                                                                        users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipeTo].using=1;
                                                                }
        
                                                }       
                                                switch(users[i].commandList[users[i].first].pipe2To){
                                                        case -1:
                                                                if(users[i].commandList[users[i].first+1].pipeIn==0){
                                                                        if(pipe(users[i].commandList[users[i].first+1].file_pipes)<0)
                                                                                return -1;
                                                                        else
                                                                                users[i].commandList[users[i].first+1].pipeIn=1;  
                                                                }
                                                        break;
                                                        case 0:
                                                        break;
                                                        default:
                                                                if(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipe2To].using==0){
                                                                        if(pipe(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipe2To].file_pipes)<0)
                                                                                return -1;
                                                                        else
                                                                                users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipe2To].using=1;
                                                                }
        
                                                }
                                                

                                                int stderrOutPut=0;
                                                ///------------------------------------------
                                                if(users[i].commandList[users[i].first].ipc_pipe!=0){
                                                					   printf("dddddddddddddddddddddddddddddddddddddddddddddddddddd\n");
                                            
                                                        		if(pipe(users[i].user_pipes)<0)return -1;
                                                        			
                                                        	//		printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
                                            
																								}
																								int tmp_pipes[2]={0,0};   
																								if(pipe(tmp_pipes)<0)return -1;
																									//int f=0;
                                                if(fork()==0){  
                                                	char root[200]="/home/wei/ras/";
                                                				//printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
                                                				        
                                                        if(users[i].first_command){
                                                        //	printf("dddddddddddddddddddddddddddddddddddddddddddddddddddd\n");
                                                //              first_command=0;
                                                                if(users[i].linepipe[users[i].line].using==1){
                                                                        dup2(users[i].linepipe[users[i].line].file_pipes[0],STDIN_FILENO);
                                                                        close(users[i].linepipe[users[i].line].file_pipes[0]);
                                                                }
                                                        }
                                                        if (users[i].commandList[users[i].first].pipeIn==1){
                                                    
                                                                dup2(users[i].commandList[users[i].first].file_pipes[0],STDIN_FILENO);
                                                                close(users[i].commandList[users[i].first].file_pipes[0]);
                                                        }else if(users[i].commandList[users[i].first].ipc_in!=0){
                                                        	
                                                        //	users[i].commandList[users[i].first].is_in=0;
                                                        			if(users[i].commandList[users[i].first].ipc_in!=-1&&users[users[i].commandList[users[i].first].ipc_in-1].haveValue!=0){
                                                        				
                                                        					users[users[i].commandList[users[i].first].ipc_in-1].haveValue=0;
                                                        				//	printf("write buffer : %d\n",strlen(users[users[i].commandList[users[i].first].ipc_in-1].buffer));
                                                        	       write(tmp_pipes[1],users[users[i].commandList[users[i].first].ipc_in-1].buffer,strlen(users[users[i].commandList[users[i].first].ipc_in-1].buffer));
																																	memset(users[users[i].commandList[users[i].first].ipc_in-1].buffer,0,strlen(users[users[i].commandList[users[i].first].ipc_in-1].buffer));
																																//	shared_stuff2[commandList[first].ipc_in-1].buffer[0]='\0';
																																	dup2(tmp_pipes[0],STDIN_FILENO);
																																	close(tmp_pipes[0]);
																																	close(tmp_pipes[1]);
																																}else {
	
                                                        					users[i].toAll=i+1;
																																	sprintf(users[i].m,"*** Error: the pipe from #%d does not exist yet. ***\n",(users[i].commandList[users[i].first].ipc_in==-1)?0:users[i].commandList[users[i].first].ipc_in);
																																	sendM();
																																	if(!checkIsExist(&(users[i].commandList[users[i].first]),root,i,users)){
                                                                				//fprintf(stderr,"Unknown command: [%s].\r\n",users[i].commandList[users[i].first].commandName);
                                                                			users[i].toAll=i+1;
                                                                			sprintf(users[i].m,"Unknown command: [%s].\r\n",users[i].commandList[users[i].first].commandName);
																																			sendM();
                                                        							}
																																	exit(0);
																																}
																												}
																											//	printf("fffffffffffffffffffffffffffffffffffff\n");
                                                        if(users[i].commandList[users[i].first].pipeTo!=0){
                                                                if(users[i].commandList[users[i].first].pipeTo==-1){
                                                                        dup2(users[i].commandList[(users[i].first+1)].file_pipes[1],STDOUT_FILENO);
                                                                        close(users[i].commandList[(users[i].first+1)].file_pipes[1]);
                                                                }else{
                                                                        dup2(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipeTo].file_pipes[1],STDOUT_FILENO);
                                                                        close(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipeTo].file_pipes[1]);
                                                                }
                                                        }else if(users[i].commandList[users[i].first].pipe2To!=0){
                                                                if(users[i].commandList[users[i].first].pipe2To==-1){
                                                                        dup2(users[i].commandList[(users[i].first+1)].file_pipes[1],STDERR_FILENO);
                                                                        //------------------project 2
                                                                        dup2(users[i].commandList[(users[i].first+1)].file_pipes[1],STDOUT_FILENO);
                                                                        //--------------------------
                                                                        close(users[i].commandList[users[i].first+1].file_pipes[1]);
                                                                }else{
                                                                        dup2(users[i].linepipe[(users[i].line+users[i].commandList[users[i].first].pipe2To)].file_pipes[1],STDERR_FILENO);
                                                                        dup2(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipe2To].file_pipes[1],STDOUT_FILENO);
                                                                        close(users[i].linepipe[users[i].line+users[i].commandList[users[i].first].pipe2To].file_pipes[1]);
                                                                }
                                                                stderrOutPut=1;
                                                        }else if(users[i].commandList[users[i].first].ipc_pipe!=0){
                                                        				//int iu=0;
                                                        			//	for(;iu<31;iu++){
                                                        					if(users[i].haveValue==1){
                                                        							sprintf(users[i].m,"*** Error: your pipe already exists. ***\n");
                                                        							users[i].toAll=i+1;
                                                        							sendM();
                                                        							//exit(0);
                                                        					}else{
                                                        	//			}
                                                        		
                                                        				//sendM();
																																dup2(users[i].user_pipes[1],STDOUT_FILENO);
																																close(users[i].user_pipes[1]);
																																close(users[i].user_pipes[0]);
																																//users[i].haveValue=1;
																															}
																												}else{
                                                                dup2(pfd[1],STDOUT_FILENO);
                                                        }
                                                        if(strlen(users[i].commandList[users[i].first].commandName)==0){
                                                                        printf("% ");
                                                                        return 0;
                                                        }
                                                        
                                                        if(users[i].commandList[users[i].first].ipc_pipe2!=0){
                                                        				//int iu=0;
                                                        			//	for(;iu<31;iu++){
                                                        				/*	if(shared_stuff2[id].havaValue==1){
                                                        							sprintf(shared_stuff2[id].m,"*** Error: your pipe already exists. ***\n");
                                                        							shared_stuff2[id].toAll=id+1;
                                                        							kill(parentid,SIGUSR1);
                                                        							exit(0);
                                                        					}*/
                                                        				//}
                                                        	     // sprintf(shared_stuff2[id].m,"*** user1 (#%d) just piped '%s' into his/her pipe. ***\n",id+1,cmd2);
                                                        				//shared_stuff2[id].toAll=32;
                                                        				printf("ooooooooooooooooooooooooo\n");
                                                        				//kill(parentid,SIGUSR1);
																																dup2(STDOUT_FILENO,STDERR_FILENO);
																																//close(shared_stuff2[id].user_pipes[1]);
																																//close(shared_stuff2[id].user_pipes[0]);
																																//shared_stuff2[id].havaValue=1;
																																stderrOutPut=1;
																												}
                                                        
                                                        if(!stderrOutPut){
                                                                dup2(pfd[1],STDERR_FILENO);
                                                        }
                                                        close(pfd[1]);
                                                        if(users[i].commandList[users[i].first].boolOutFile==1){
                                                                char fileStrTmp[1024]="/home/wei/ras/";
                                                                strcat(fileStrTmp,users[i].commandList[users[i].first].outFile);
                                                                int oldfd =open(fileStrTmp,(O_RDWR|O_CREAT),0644);
                                                                dup2(oldfd,1);
                                                                close(oldfd);
                                                        }
                                                        char *argv2[200];
                                                        
                                                        if(!checkIsExist(&(users[i].commandList[users[i].first]),root,i,users)){
                                                                fprintf(stderr,"Unknown command: [%s].\r\n",users[i].commandList[users[i].first].commandName);
                                                                return -1;
                                                        }
                                                        int tmp=0;
                                                        int tmp_i;
                                                        //char tmp_str[1000]="/home/wei/ras/";
                                                        char argv4[20][200];
                                                        for(tmp_i=0;;tmp_i++){
                                                                if(tmp_i==0){
                                                                        argv2[tmp++]=(char *)&(users[i].commandList[users[i].first].commandName);
                                                                        continue;
                                                                }
                                                                if(tmp_i==1){
                                                                        if(strlen(users[i].commandList[users[i].first].argList)!=0)
                                                                                argv2[tmp++]=(char *)&(users[i].commandList[users[i].first].argList);
                                                                        continue;
                                                                }
                                                                char strsept[]=" \t";
                                                                char *str_ptr=strtok(users[i].commandList[users[i].first].argv3,strsept);
                                                                int i=0;        
                                                            while(str_ptr!=NULL){
                                                                strncpy(argv4[i],str_ptr,200);
                                                                argv2[tmp++]=(char *)&argv4[i++];
                                                                str_ptr=strtok(NULL,strsept);
                                                                }
                                                                break;
                                                                
                                                        }
                                                        argv2[tmp]=(char *)0;
                                                        close(pfd[0]);
                                                        execv(root,(char * const*)argv2);
                                                        fprintf(stderr,"%s exe error\r\n",argv2[1]);
                                                        exit(0);
                                                }else {
                                                	
																												close(tmp_pipes[1]);
                                                        wait(&statval);
                                                        
                                                        close(tmp_pipes[0]);
 
                                                        
                                                        if(users[i].commandList[users[i].first].ipc_pipe!=0&&users[i].haveValue==0){
                                                        	sprintf(users[i].m,"*** %s (#%d) just piped '%s' into his/her pipe. ***\n",users[i].name,i+1,cmd2);
                                                        	users[i].toAll=32;
                                                        	sendM2All(users,users[i].m,1);
                                                        	//sendM();
                                                        	users[i].toAll=0;
                                                        	close(users[i].user_pipes[1]);
                                                        	users[i].haveValue=1;
                                                        	
                                                       // 	if(strlen(users[i].buffer)!=0){
                                                       // 		char tttt[1024];
                                                       // 		read(users[i].user_pipes[0],tttt[1024],1024);
                                                       // 		strcat(users[i].buffer,tttt);
                                                        //		close(users[i].user_pipes[0]);
                                                       // }else{
                                                        	read(users[i].user_pipes[0],users[i].buffer,4095);
                                                        	 close(users[i].user_pipes[0]);
                                                        //	}
                                                        	//printf(users[i].buffer);
                                                        }
                                                       

                          
                                                       
                                                       
                                                        if(users[i].first+1==users[i].comIndex){
                                                                if(users[i].linepipe[users[i].line+1].using==1){
                                                                        close(users[i].linepipe[users[i].line+1].file_pipes[1]);  
                                                                }
                                                                if(users[i].linepipe[users[i].line].using==1)
                                                                        close(users[i].linepipe[users[i].line].file_pipes[0]);
                                                        }
                                                        if(users[i].commandList[users[i].first].pipeTo==-1||users[i].commandList[users[i].first].pipe2To==-1)
                                                                        close(users[i].commandList[users[i].first+1].file_pipes[1]);
                                                        close(pfd[1]);          
                                                        users[i].first_command=0;

                                                if(users[i].commandList[users[i].first].pipeIn){
                                                        close(users[i].commandList[(users[i].first)].file_pipes[0]);
                                                }

                                                                int len=read(pfd[0],buffer,4095);
                                                                if(strlen(buffer)!=0){
                                                                        if(buffer[len-1]!='\n')
                                                                                strcat(buffer,"\r\n");
                                                                        buffer[len+4]='\0';
                                                                }

                                                                write(users[i].socket_fd,buffer,strlen(buffer));
                                                                sendM();
                                              if(users[i].commandList[users[i].first].ipc_in!=-1&&users[i].commandList[users[i].first].ipc_in!=0){
                                                        			if(users[users[i].commandList[users[i].first].ipc_in-1].haveValue!=0){
                                                        							sprintf(users[i].m,"*** %s (#%d) just received the pipe from %s (#%d) by '%s' ***\n",users[i].name,i+1,users[users[i].commandList[users[i].first].ipc_in-1].name,users[i].commandList[users[i].first].ipc_in,cmd2);
                                                        							users[i].toAll=32;
                                                        							users[users[i].commandList[users[i].first].ipc_in-1].haveValue=0;
                                                        							//sendM();
                                                        							 sendM2All(users,users[i].m,1);
                                                        							 users[i].toAll=0;
                                                        			   // write(tmp_pipes[1],users[users[i].commandList[users[i].first].ipc_in-1].buffer,strlen(users[users[i].commandList[users[i].first].ipc_in-1].buffer));
																																	memset(users[users[i].commandList[users[i].first].ipc_in-1].buffer,0,strlen(users[users[i].commandList[users[i].first].ipc_in-1].buffer));
																																}//else {
																																	//sprintf(users[i].m,"*** Error: the pipe from #%d does not exist yet. ***",users[i].commandList[users[i].first].ipc_in);
																																	//users[i].toAll=i;
																																	//sendM();
																																//}
                                                       
                                                      }
                                                                usleep(500);
                                                /*      }else*/ if((users[i].first+1)==users[i].comIndex){
                                                                write(users[i].socket_fd,"% ",2);
                                                        }
                                                                close(pfd[0]);
                                                        memset(&(users[i].commandList[users[i].first]),0,sizeof(struct commandInfo));
                                                }
                                        }
                                        users[i].first++;
                                        users[i].first%=10000;
                                }
                            }    
        				}
        			
        			
        		}
        	}
        }
      }
        	